package com.gao.weblog.admin.controller;

import com.gao.weblog.admin.model.vo.article.ArticleDetailVO;
import com.gao.weblog.admin.model.vo.article.PublishArticleDTO;
import com.gao.weblog.admin.model.vo.article.ArticleVO;
import com.gao.weblog.admin.service.AdminArticleService;
import com.gao.weblog.common.aspect.ApiOperationLog;
import com.gao.weblog.common.domain.dos.ArticleDO;
import com.gao.weblog.common.model.dto.ArticleQueryDTO;
import com.gao.weblog.common.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/article")
@Api(tags = "后台文章管理")
public class AdminArticleController {
    @Autowired
    private AdminArticleService articleService;

    @PostMapping("/list")
    @ApiOperation(value = "文章列表")
    @ApiOperationLog(description = "分页查询文章")
    public Response findArticleList(@RequestBody @Validated ArticleQueryDTO articleQueryDTO){

        List<ArticleDO> articleDOS =  articleService.findArticlePageList(articleQueryDTO);

        return Response.success(articleDOS);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "文章详情")
    @ApiOperationLog(description = "查询文章详情")
    public Response findArticleDetail(@RequestBody @Validated Long articleId){

        ArticleDetailVO articleDetail = articleService.findArticleDetail(articleId);

        return Response.success(articleDetail);

    }

    @PostMapping("/publish")
    @ApiOperation(value = "发布文章")
    @ApiOperationLog(description = "发布文章")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response publishArticle(@RequestBody @Validated PublishArticleDTO publishArticleDTO){

        articleService.publishArticle(publishArticleDTO);

        return Response.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除文章")
    @ApiOperationLog(description = "删除文章")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response deleteArticle(@RequestBody @Validated Long articleId){

        return Response.success(articleService.deleteArticle(articleId));

    }

    @PostMapping("/modefied")
    @ApiOperation(value = "编辑文章")
    @ApiOperationLog(description = "编辑文章")
    public Response<ArticleVO> updateArticle(@RequestBody @Validated ArticleVO articleVO){

        return Response.success(articleService.updateArticle(articleVO));
    }
}
